from django.db import models
from user.models import Cashier

# 系统设置模型
class SystemSetting(models.Model):
    key = models.CharField(
        max_length=100,
        unique=True,
        verbose_name='设置键'
    )
    value = models.TextField(
        verbose_name='设置值'
    )
    description = models.CharField(
        max_length=255,
        blank=True,
        null=True,
        verbose_name='描述'
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间'
    )

    class Meta:
        verbose_name = '系统设置'
        verbose_name_plural = '系统设置管理'
        db_table = "backend_systemsetting"

    def __str__(self):
        return self.key

# 操作日志模型
class ActivityLog(models.Model):
    ACTION_LOGIN = 'login'
    ACTION_LOGOUT = 'logout'
    ACTION_CREATE = 'create'
    ACTION_UPDATE = 'update'
    ACTION_DELETE = 'delete'
    ACTION_VIEW = 'view'
    
    ACTION_TYPES = [
        (ACTION_LOGIN, '登录'),
        (ACTION_LOGOUT, '登出'),
        (ACTION_CREATE, '创建'),
        (ACTION_UPDATE, '更新'),
        (ACTION_DELETE, '删除'),
        (ACTION_VIEW, '查看'),
    ]
    
    cashier = models.ForeignKey(
        Cashier,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='activity_logs',
        verbose_name='操作人'
    )
    action_type = models.CharField(
        max_length=20,
        choices=ACTION_TYPES,
        verbose_name='操作类型'
    )
    resource_type = models.CharField(
        max_length=100,
        blank=True,
        null=True,
        verbose_name='资源类型'
    )
    resource_id = models.IntegerField(
        blank=True,
        null=True,
        verbose_name='资源ID'
    )
    details = models.TextField(
        blank=True,
        null=True,
        verbose_name='操作详情'
    )
    ip_address = models.GenericIPAddressField(
        blank=True,
        null=True,
        verbose_name='IP地址'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='操作时间'
    )

    class Meta:
        verbose_name = '操作日志'
        verbose_name_plural = '操作日志管理'
        ordering = ['-created_at']
        db_table = "backend_activitylog"

    def __str__(self):
        return f'{self.cashier.username if self.cashier else "未知"} - {self.get_action_type_display()} - {self.created_at}'

# 系统通知模型
class Notification(models.Model):
    title = models.CharField(
        max_length=200,
        verbose_name='通知标题'
    )
    content = models.TextField(
        verbose_name='通知内容'
    )
    is_read = models.BooleanField(
        default=False,
        verbose_name='是否已读'
    )
    recipient = models.ForeignKey(
        Cashier,
        on_delete=models.CASCADE,
        related_name='notifications',
        verbose_name='接收人'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间'
    )

    class Meta:
        verbose_name = '系统通知'
        verbose_name_plural = '系统通知管理'
        ordering = ['-created_at']
        db_table = "backend_notification"

    def __str__(self):
        return self.title
